package com.labichaoka.chaoka.ui.home.fragment.my.withdraw;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.base.BaseActivity;
import com.labichaoka.chaoka.entity.GetWithdrawInfo;
import com.labichaoka.chaoka.entity.GoWithdrawRequest;
import com.labichaoka.chaoka.entity.GoWithdrawResponse;
import com.labichaoka.chaoka.ui.home.fragment.my.record.WithDrawRecordActivity;
import com.labichaoka.chaoka.ui.web.WebViewActivity;
import com.labichaoka.chaoka.utils.DetectionUtil;
import com.labichaoka.chaoka.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class WithdrawActivity extends BaseActivity implements WithdrawView{

    @BindView(R.id.bank_logo)
    ImageView bankLogo;
    @BindView(R.id.bank_name)
    TextView bankName;
    @BindView(R.id.amount)
    TextView amount;
    @BindView(R.id.next1)
    TextView next1;
    @BindView(R.id.txt)
    TextView txt;
    @BindView(R.id.right_txt)
    TextView rightTxt;
    @BindView(R.id.reset)
    LinearLayout reset;

    private WithdrawPresenter presenter;
    private String appJumpUrl;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_withdraw;
    }

    @Override
    protected void initView() {
        rightTxt.setVisibility(View.VISIBLE);
        next1.setTypeface(iconfont);
        txt.setText(Html.fromHtml(this.getString(R.string.withdraw_tip)));
    }

    @Override
    protected void initData() {
        presenter=new WithdrawPresenterImpl(new WithdrawInteractorImpl(),this);
        presenter.getWithdrawInfo();
    }

    @OnClick({R.id.commit,R.id.reset,R.id.right_txt})
    public void click(View v){
        switch (v.getId()){
            case R.id.commit:
                GoWithdrawRequest request=new GoWithdrawRequest();
                request.setCurrentPageUrl("https://cscard.labifenqi.com/blankPage");
                presenter.goWithdraw(request);
                break;
            case R.id.reset:
                resetPwd();
                break;
            case R.id.right_txt:
                gotoActivity(mContext,WithDrawRecordActivity.class,null);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void showProgress() {
        showLoadingProgress();
    }

    @Override
    public void hideProgress() {
        hideLoadingProgress();
    }

    @Override
    public void showMessage(String str) {
        ToastUtils.show(str);
    }

    @Override
    public void getWithdrawInfoCallback(GetWithdrawInfo response) {
        if (response==null){
            ToastUtils.show("获取数据异常，请稍后重试！");
            return;
        }
        appJumpUrl=response.getData().getAppJumpUrl();
        Glide.with(mContext).load(response.getData().getLogoUrl()).into(bankLogo);
        String amountStr=response.getData().getAmount();
        if (!TextUtils.isEmpty(amountStr)){
            amount.setText(DetectionUtil.addComma(amountStr));
        }
        String accountBankGroup=response.getData().getAccountBankGroup();
        bankName.setText(accountBankGroup);
        String isCanChangePsd=response.getData().getIsCanChangePsd();
        if (!TextUtils.isEmpty(isCanChangePsd)&&isCanChangePsd.equals("0")){
            //不可修改密码
            reset.setVisibility(View.GONE);
        }else{
            reset.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void goWithdrawCallback(GoWithdrawResponse response) {
        if (response==null){
            ToastUtils.show("获取数据异常，请稍后重试！");
            return;
        }
        String url=response.getData().getWithdrawUrl();
        Bundle bundle=new Bundle();
        bundle.putString("type","9");
        bundle.putString("url",url);
        gotoActivity(mContext,WebViewActivity.class,bundle);
    }

    public void resetPwd(){
        Bundle bundle=new Bundle();
        bundle.putString("type","8");
        bundle.putString("url",appJumpUrl);
        gotoActivity(mContext,WebViewActivity.class,bundle);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }
}
